package migrations

import (
	"github.com/goravel/framework/contracts/database/schema"
	"github.com/goravel/framework/facades"
)

type M20250701093210CreateSysDictTable struct {
}

// Signature The unique signature for the migration.
func (r *M20250701093210CreateSysDictTable) Signature() string {
	return "20250701093210_create_sys_dict_table"
}

// Up Run the migrations.
func (r *M20250701093210CreateSysDictTable) Up() error {
	var err error
	if !facades.Schema().HasTable("sys_dict") {
		err = facades.Schema().Create("sys_dict", func(table schema.Blueprint) {
			table.ID()
			table.String("name").Comment("名称")
			table.String("slug").Comment("标识")
			table.String("remark").Comment("说明")
			table.Integer("status").Default(0).Comment("状态")
			table.TimestampsTz()
			table.Unique("slug")
		})
		if err != nil {
			return err
		}
	}

	if !facades.Schema().HasTable("sys_dict_data") {
		err = facades.Schema().Create("sys_dict_data", func(table schema.Blueprint) {
			table.ID()
			table.String("dict_slug").Comment("字典标识")
			table.String("label").Comment("标识")
			table.String("value").Comment("值")
			table.Integer("order_num").Default(0).Comment("排序")
			table.TinyInteger("is_default").Default(0).Comment("是否默认")
			table.TinyInteger("status").Default(0).Comment("状态")
			table.String("remark").Comment("描述")
			table.TimestampsTz()

			table.Unique("dict_slug", "value")
		})
		if err != nil {
			return err
		}
	}

	return nil
}

// Down Reverse the migrations.
func (r *M20250701093210CreateSysDictTable) Down() error {
	_ = facades.Schema().DropIfExists("sys_dict")
	_ = facades.Schema().DropIfExists("sys_dict_data")
	return nil
}
